<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Returns a copy, sorted by key</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="ds-map.ksort.html">« Ds\Map::ksort</a></li>
      <li style="float: right;"><a href="ds-map.last.html">Ds\Map::last »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.ds-map.html">Map</a></li>
    <li>Returns a copy, sorted by key</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="ds-map.ksorted" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Ds\Map::ksorted</h1>
  <p class="verinfo">(No version information available, might only be in Git)</p><p class="refpurpose"><span class="refname">Ds\Map::ksorted</span> &mdash; <span class="dc-title">Returns a copy, sorted by key</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-ds-map.ksorted-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Ds\Map::ksorted</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$comparator</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="class.ds-map.html" class="type Ds\Map">Ds\Map</a></span></div>

  <p class="para rdfs-comment">
    Returns a copy sorted by key, using an optional <code class="parameter">comparator</code> function.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-ds-map.ksorted-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
     <dt>
<code class="parameter">comparator</code></dt>

     <dd>

      <p class="para">
       在第一个参数小于，等于或大于第二个参数时，该比较函数必须相应地返回一个小于，等于或大于 0 的整数。
      </p>
       <div class="methodsynopsis dc-description"><span class="methodname"><span class="replaceable">callback</span></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$a</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$b</code></span>): <span class="type">int</span></div>

      <div class="caution"><strong class="caution">警告</strong>
       <p class="para">
        Returning <em class="emphasis">non-integer</em> values from the comparison
        function, such as <span class="type">float</span>, will result in an internal cast to
        <span class="type">int</span> of the callback&#039;s return value. So values such as
        0.99 and 0.1 will both be cast to an integer value of 0, which will
        compare such values as equal.
       </p>
      </div>
     </dd>

    
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-ds-map.ksorted-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
    Returns a copy of the map, sorted by key.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-ds-map.ksorted-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-4092">
   <p><strong>示例 #1 <span class="function"><strong>Ds\Map::ksorted()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$map&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;\</span><span style="color: #0000BB">Ds</span><span style="color: #007700">\</span><span style="color: #0000BB">Map</span><span style="color: #007700">([</span><span style="color: #DD0000">"b"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"c"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$map</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">ksorted</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Ds\Map Object
Ds\Map Object
(
    [0] =&gt; Ds\Pair Object
        (
            [key] =&gt; a
            [value] =&gt; 1
        )

    [1] =&gt; Ds\Pair Object
        (
            [key] =&gt; b
            [value] =&gt; 2
        )

    [2] =&gt; Ds\Pair Object
        (
            [key] =&gt; c
            [value] =&gt; 3
        )

)
</pre></div>
   </div>
  </div>
  <div class="example" id="example-4093">
   <p><strong>示例 #2 <span class="function"><strong>Ds\Map::ksorted()</strong></span> example using a comparator</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$map&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;\</span><span style="color: #0000BB">Ds</span><span style="color: #007700">\</span><span style="color: #0000BB">Map</span><span style="color: #007700">([</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"x"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"y"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"z"</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">//&nbsp;Reverse<br /></span><span style="color: #0000BB">$sorted&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$map</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">ksorted</span><span style="color: #007700">(function(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">&lt;=&gt;&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$sorted</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Ds\Map Object
Ds\Map Object
(
    [0] =&gt; Ds\Pair Object
        (
            [key] =&gt; 2
            [value] =&gt; y
        )

    [1] =&gt; Ds\Pair Object
        (
            [key] =&gt; 1
            [value] =&gt; x
        )

    [2] =&gt; Ds\Pair Object
        (
            [key] =&gt; 0
            [value] =&gt; z
        )

)
</pre></div>
   </div>
  </div>

 </div>



</div></div></div></body></html>